﻿using System.Collections.Generic;
using GenericSimulator;
using VEGA.Algorithm.Elements;

namespace VEGA.Algorithm
{
    class Crosser
    {
        public List<Individual> Input { get; set; }
        public List<Individual> Output { get; set; }

        public void Run(ISimulator simulator)
        {
            Output = new List<Individual>();
            for (int i = 0; i < Input.Count; i += 2)
            {
                var parentOne = Input[i];
                var parentTwo = Input[i + 1];

                var children = GetChildren(parentOne, parentTwo, simulator);

                Output.AddRange(children);
            }
        }

        private IEnumerable<Individual> GetChildren(Individual parentOne, Individual parentTwo, ISimulator simulator)
        {

            var parameters = simulator.ChangebleParameters;
            var parentOneParametersToTakeCount = parameters.Count/2;
            var parentTwoParametersToTakeCount = parameters.Count - parentOneParametersToTakeCount;

            var childOneConfig = simulator.GetEmptyConfigurationInstance();
            var childTwoConfig = simulator.GetEmptyConfigurationInstance();

            for (int i = 0; i < parentOneParametersToTakeCount; i++)
            {
                childOneConfig.Add(parameters[i], parentOne.Configuration.GetString(parameters[i]));
                childTwoConfig.Add(parameters[i], parentTwo.Configuration.GetString(parameters[i]));
            }

            for (int i = parentOneParametersToTakeCount; i < parentOneParametersToTakeCount+parentTwoParametersToTakeCount; i++)
            {
                childOneConfig.Add(parameters[i], parentTwo.Configuration.GetString(parameters[i]));
                childTwoConfig.Add(parameters[i], parentOne.Configuration.GetString(parameters[i]));
            }

            var childOne = new Individual(childOneConfig);
            var childTwo = new Individual(childTwoConfig);

            return new List<Individual>() {childOne, childTwo};
        }
    }
}
